package com.oscar.web.service;

import com.alibaba.fastjson.JSON;
import com.oscar.web.dao.UserBookEvaluateRepository;
import com.oscar.web.domain.rating;
import com.oscar.web.util.LogData;
import io.netty.util.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Iterator;

@Service
public class UserBookEvaluateService {

    Logger logger= LoggerFactory.getLogger(UserBookEvaluateService.class);

    @Autowired
    private UserBookEvaluateRepository userBookEvaluateRepository;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private RedisProperties.Jedis jedis;

    public String saveValue(rating userBookEvaluate){
        String msg="感谢评价，您的评价是我们提升的航向。";
        if (userBookEvaluateRepository.existsByUserIdAndBookId(userBookEvaluate.getUserId(),userBookEvaluate.getBookId())){
            msg="该图书您已评价了哦，无需再次评价。";
            System.out.println("图书重复评价");
        }else{
            userBookEvaluateRepository.save(userBookEvaluate);
            System.out.println("王成图书评价");
        }
        //将页面信息通过JSON.toJSON(logData).toString() 方法封装成字符串
        logger.info("PRODUCT_RATING_PREFIX" + ":" + userBookEvaluate.getUserId() +"|"+ userBookEvaluate.getBookId() +"|"+ userBookEvaluate.getScore() +"|"+ System.currentTimeMillis()/1000);
        redisTemplate.boundListOps("userId:" +userBookEvaluate.getUserId()).leftPush(userBookEvaluate.getBookId() +":"+ userBookEvaluate.getScore());
        return msg;
    }

}
